home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / graphics / improc40.zip / PRIMER.DOC < prev    next >
Text File  |  1992-06-16  |  21KB  |  465 lines

  1.      IMPROCES(C). Copyright John Wagner 1991. All rights reserved.
  2.  
  3.                    An Image Processing and VGA Primer
  4.  
  5.  
  6. This article may only be distributed as part of the IMPROCES, Image
  7. Processing Software package by John Wagner. IMPROCES is used for the
  8. examples and it is assumed the reader has a copy of the program. This
  9. article may not be reproduced in any manner without prior permission
  10. from the author.
  11.  
  12. The Image and The Screen:
  13. Images are represented as a series of points on a surface of varying
  14. intensity. For example, on a monochrome or black and white photograph,
  15. the points on the image are represented with varying shades of grey. On
  16. a computer screen, these points are called pixels. Pixels on the screen
  17. are mapped into a two dimensional coordinate system that starts at the
  18. top, left corner with the coordinate 0,0. The coordinates in the X
  19. direction refer to pixels going in the right (horizontal) direction and
  20. coordinates in the Y direction refer to pixels going in the down
  21. (vertical) direction. The coordinates X,Y are used to define a specific
  22. pixel on the screen. When a computer image is said to have a resolution
  23. of 320x200x256, it means that the image has a X width of 320 pixels, a Y
  24. length of 200 pixels and contains 256 colors.
  25.  
  26.           Pixel Mapping on 320x200 video screen:
  27.  
  28.                        0,0          319,0
  29.                          X-------->
  30.                          Y
  31.                          |
  32.                          |
  33.                          V
  34.                        0,199        319,199
  35.  
  36. Color:
  37. A shade of grey is defined as having equal levels of Red, Green and Blue
  38. (RGB). A color image however is represented as having points that are
  39. represented by varying levels of RGB. NOTE: The color model of RGB is
  40. just one way of representing color. Conveniently it is also the model
  41. that computers use when representing colors on a screen. For that reason,
  42. it will be the model we will use here!
  43.  
  44. When splitting up a color into its RGB components, it is common to use a
  45. number between 0 and 1 (or percentage of total color) to represent the
  46. colors RGB intensities. R=0, G=0, B=0 (usually shown as 0,0,0) would be
  47. the absence of all color (black) and 1, 1, 1 would be full intensity for
  48. all colors or white. .5, 0, 0 would be half red, while 0, .25, 0 would
  49. be one quarter green. Using this model, an infinite amount of colors are
  50. possible. Unfortunately, personal computers of this day and age are not
  51. capable of handling an infinite amount of colors and must use
  52. approximations when dealing with color.
  53.  
  54. The VGA:
  55. With the advent of the VGA video subsystem for IBM PC's and compatibles,
  56. Image Processing is now available to the home PC graphics enthusiast.
  57. Also, since this article is meant to be used with IMPROCES, it will help
  58. to understand the limitations of hardware we are working with. The VGA
  59. can display 256 colors at one time out of a possible 262,144. The number
  60. 262,144 is not a magical number. It comes from the limitations of the
  61. VGA hardware itself. The VGA (and many common graphics subsystems)
  62. represent their 256 colors in a look up table of RGB values so that the
  63. memory where the video display is mapped need only keep track of one
  64. number (the look up value) instead of three (see diagram 1). The look up
  65. table of the VGA allows for 64 (0 to 63) levels of RGB for each color.
  66. This means that 64 to the 3rd power of different colors are possible.
  67. 64^3=262,144. Because only 64 levels of RGB are possible, it is only
  68. possible to represent 64 shades of grey with a VGA. Remember that a
  69. shade of grey is defined as equal levels of RGB. There is also a
  70. disparity in the common usage of values from 0 to 1 to define a level of
  71. RGB. However, this problem is easily solved by dividing the VGA look up
  72. table RGB number by 64 to get its proper percentage of the total color
  73. (exp. 32/64=.5 or 50% of total color).
  74.  
  75.         Diagram 1. Sample Color Look Up Table (LUT) for VGA
  76.  
  77.                    Color      R   G   B
  78.                      1        0   23  56
  79.                      2        34  24  45
  80.                      3        23  12  43
  81.                      ....
  82.                      254      13  32  43
  83.                      255      12  63  12
  84.  
  85. Another important aspect of the VGA is that it not only allows up to 256
  86. colors to displayed at one time. There are now Super VGA cards that
  87. allow you to work in display resolutions up to 1024x768 with 256 colors.
  88. These Super VGA cards have more memory on board so they can handle the
  89. higher resolutions. It is important to understand that the amount of
  90. memory on the video board will determine the highest resolution it can
  91. handle.
  92.  
  93. Video Memory is bitmapped, in a 256 color mode, one pixel requires one
  94. byte of memory as a byte can hold a value from 0 to 255. VGA video
  95. memory begins at the hexidecimal address A0000000. The VGA video memory
  96. area is 64K in length. Because of the length of the VGA memory area, the
  97. maximum resolution a standard VGA card can achieve is 320x200x256. This
  98. is because 320 pixels (bytes) times 200 pixels (bytes) equals 62,000
  99. bytes (62.5K) and fills the video memory area.
  100.  
  101.                 pixel 0,0───┐ 0,1  0,2
  102.                             │  │    │
  103.     Memory Address A0000000 ┘  │    │
  104.                    A0000001 ───┘    │
  105.                    A0000002 ────────┘
  106.  
  107. But wait a second, how can we get video modes up to 1024x768? If you
  108. want a resololution of 1024x768x1byte(256 colors), you require 1024x768
  109. bytes of video memory, 786,432 bytes, or 768K. Because the architecture
  110. of the PC only allows for a maximum of 64K of VGA memory, a Super VGA
  111. card maintains its own pool of memory that is displayed to the screen
  112. and swaps memory in and out of the 64K "proper" VGA video memory address
  113. space so that programs can write to it.
  114.  
  115. IMPROCES Stuff:
  116. IMPROCES image processing functions all work in a defined area called the
  117. WORK AREA. The default work area starts at the top-left corner of the
  118. screen and ends at pixel 196 in the X (horizontal) direction and at
  119. pixel 165 in the Y (vertical) direction. These aren't magical numbers
  120. either. A friend of mine lent me a CCD device to capture greyscale
  121. images and process them with IMPROCES. The size of the image the CCD
  122. device output was, you guessed it, 196x165. You can change the WORK AREA
  123. by selecting WORK AREA from the ENHANCE pull down menu and then defining
  124. a new rectanglar area for IMPROCES to use.
  125.  
  126. The Histogram:
  127. With all of that out of the way, lets examine a few things we can learn
  128. from an image without actually modifying it. A tool that is commonly
  129. used to determine the overall contrast of an image is the Histogram. A
  130. histogram is defined as the measure of the distribution of a defined
  131. set. As you probably know, histograms are not unique to image
  132. processing.
  133.  
  134. The histogram takes a count of all the values on the image and
  135. displays them graphically. When I say a count, I mean how many pixels
  136. that contain the color in LUT 0 through 255. The count of a color is
  137. called that colors BIN.
  138.  
  139. To get a histogram in IMPROCES, select AREA HISTO from the ENHANCE pull
  140. down menu. The histogram is displayed from the left to the right
  141. starting at color 0 and working over a column at a time to color 255.
  142. The BIN's are displayed as lines going upward. You can move the mouse to
  143. a desired BIN and click on that column to get that columns exact count,
  144. which is shown in the lower right corner. You can also press 'S' to save
  145. the histogram to an ASCII file that you can examine later.
  146.  
  147. Assuming that the image is a greyscale image, the histogram shows us
  148. the overall contrast of the image by how much of the greyscale is covered
  149. by the image. A high contrast image will cover most of the greyscale while
  150. a low contrast image will only cover a small portion of the greyscale.
  151.  
  152.         Examples of Histograms:
  153.  
  154.                     High Contrast Image:
  155.  
  156.                                       -100  x6
  157.                              │││      -
  158.                            │ ││││     -
  159.                           ││││││││    -
  160.                           │││││││││   -
  161.                          │││││││││││  -
  162.                          └┴┴┴┴┴┴┴┴┴┘  -0
  163.                         0          255
  164.  
  165.   
  166.                     Low Contrast Image:
  167.  
  168.                                       -100  x8
  169.                              │││      -
  170.                              ││││     -
  171.                              ││││     -
  172.                              ││││     -
  173.                              ││││     -
  174.                          ────┴┴┴┴───  -0
  175.                         0          255
  176.  
  177. Contrast Enhancement:
  178. Contrast enhancement is one of the easiest to understand of the image
  179. processing functions. As of version 3.0 of IMPROCES, contrast stretching
  180. will only work properly on images with a greyscale palette. Future
  181. versions of IMPROCES will probably allow for contrast stretching of
  182. color images. Contrast Stretching will take a portion of the greyscale
  183. and stretch it so that it covers a wider portion of the greyscale. To do
  184. this, you must first define an area of the greyscale that you would like
  185. to stretch. IMPROCES provides three ways to do this. All of the methods
  186. use two variables, one called Low_CLIP (L_CLIP) and one called the
  187. High_CLIP (H_CLIP). Depending on which method you use, the variables
  188. will be used in different ways.
  189.  
  190. When using the CNTR STRTCH method, the first BIN working up from 0 that
  191. contains more pixels then the value of L_CLIP will become the color 0
  192. (black), any BINS below that value are set to 0 as well. The first BIN
  193. working down from 255 that contains more pixels then the value of H_CLIP
  194. will become the value 255 (white) and any BINS above that will become
  195. 255 as well. All of the BINS in between will be remapped between 0 and
  196. 255 by a ratio of where they where in respect to the original LOW and
  197. HIGH CLIP values.
  198.  
  199.           Take the original low contrast image:
  200.  
  201.                     Low Contrast Image:
  202.      
  203.                                       -100  x8
  204.                              │││      -
  205.                              ││││     -
  206.                              ││││     -
  207.                              ││││     -
  208.                              ││││     -
  209.                          ────┴┴┴┴───  -0
  210.                         0          255
  211.  
  212.    L_CLIP and H_CLIP are both set to 30 so the L_CLIP and H_CLIP will
  213.    hit at these points:
  214.  
  215.                                       -100  x8
  216.                              │││      -
  217.                              ││││     -
  218.                              ││││     -
  219.                              ││││     -
  220.                              ││││     -
  221.                          ────┴┴┴┴───  -0
  222.                         0    |  |  255
  223.                         L_CLIP  H_CLIP
  224.  
  225.  
  226.    These BINS will now be reset to 0 and 255 and the BINS in between are
  227.    set in respect to there original location to the L_CLIP and H_CLIP
  228.    BINS:
  229.  
  230.                     Constrast Stretched Image:
  231.  
  232.                                       -100  x8
  233.                          │  │   │     -
  234.                          │  │   │  │  -
  235.                          │  │   │  │  -
  236.                          │  │   │  │  -
  237.                          │  │   │  │  -
  238.                          └──┴───┴──┘  -0
  239.                         0          255
  240.                         L_CLIP  H_CLIP
  241.  
  242.    The resulting image will have its contrast stretched across the entire
  243.    greyscale, resulting in a higher contrast image.
  244.  
  245. The standard CNTR STRCH works well for a lot of images. The problem is,
  246. rarely will an image be spread so evenly across the greyscale to begin
  247. with. A lot of times there will be spikes in the histogram at either
  248. end that you might want to remove.
  249.  
  250.                    Histogram with spikes:
  251.  
  252.                                       -100  x6
  253.                            │   │  │   -
  254.                           ││  │││ │   -
  255.                           ││ ││││ │   -
  256.                           │││││││││   -
  257.                           │││││││││   -
  258.                          ─┴┴┴┴┴┴┴┴┴─  -0
  259.                         0          255
  260.  
  261. Using the standard contrast stretch, you would not be able to get over
  262. these spikes if you wanted to stretch the middle of the histogram.
  263. IMPROCES provides the CNTR VSTCH for this purpose. This method uses the
  264. variables L_CLIP and H_CLIP to pick which BIN you want to be the L_CLIP
  265. and H_CLIP values, without regard to their values. The only thing to
  266. remember is not to set the L_CLIP higher then the H_CLIP, otherwise the
  267. program will give you an error message. Besides the difference in the
  268. way the program uses the variables, VSTCH works identical to STRCTH.
  269.  
  270. CNTR LSTRCH works the same as VSTCH in the respect that the variables
  271. are used to pick the L_CLIP and H_CLIP values. The difference is that
  272. the BINS that are below the L_CLIP are not set to 0, they are left alone
  273. and the same for the BINS above the H_CLIP value are left alone, not set
  274. to 255. Only the BINS in between L_CLIP and H_CLIP are stretched between
  275. 0 and 255.
  276.  
  277. Convolution:
  278. Convolution is a another common method of processing an image. It uses a
  279. pixels neighbors as a function of its new value. The main points of
  280. convolution can be explained rather easily:
  281.  
  282.    A matrix is decided upon that contains certain values. This matrix is
  283.    called a Kernel. The kernel is then passed over the image from left
  284.    to right, top to bottom, with the value of the center pixel being
  285.    replaced with the sum of the product of the kernel values and the
  286.    pixels under them.
  287.  
  288. A kernel with an odd number for its width and length is used. IMPROCES
  289. uses a 3x3 kernel:
  290.  
  291.                      ┌───┬───┬───┐
  292.                      ├───┼───┼───┤
  293.                      ├───┼───┼───┤
  294.                      └───┴───┴───┘
  295.  
  296. An odd length and width is used so that there will be center point on the
  297. kernel. Values are then assigned to the kernel:
  298.                      
  299.                     Sharpening kernel:
  300.                       -1  -1  -1
  301.                       -1   9  -1
  302.                       -1  -1  -1
  303.  
  304. The kernel is then passed over the image from left to right, top to
  305. bottom. The kernal is then multiplied, point by point with the
  306. with pixels in the 3x3 section of the image under it. The products
  307. are then summed and the middle pixel in the image under the kernal
  308. is then replaced with this new value.
  309.  
  310.                     Sharpening kernel:
  311.                       -1  -1  -1
  312.                       -1   9  -1
  313.                       -1  -1  -1
  314.  
  315.               Area of image being processed:
  316.                        23  34  25
  317.                        23  43  21
  318.                        23  43  43
  319.  
  320.                      Product of values:
  321.                       -23 -34 -25
  322.                       -23 387 -21
  323.                       -23 -43 -43
  324.  
  325.                     Sum of products:
  326.                          156
  327.                        
  328.                  Convoled Area of image:
  329.                        23  34  25
  330.                        23 156  21
  331.                        23  43  43
  332.  
  333. The new value of the center pixel is then written to the screen. You
  334. should note that the output from the previous operation is not used for
  335. input in the next operation. The input and output image must be treated
  336. separately. IMPROCES does this operation in place by using two rotating
  337. three line buffers. The resulting image is said to have convoled from
  338. the original.
  339.  
  340. The example shows a sharpening kernel. A sharpening kernel is actually a
  341. Laplacian kernel with the original image added back in. The laplacian is
  342. an edge detecting kernal, so when you detect the edges of the image and
  343. then add the original image back in, you sharpen the edges, thereby improving
  344. the sharpness of the image. A laplacian kernel looks like so:
  345.                   
  346.                     Laplacian kernel:
  347.                       -1  -1  -1
  348.                       -1   8  -1
  349.                       -1  -1  -1
  350.  
  351. You will notice that the center value for the laplacian is an 8 and the
  352. sum of the kernel is 0. What this does to an image is make areas that
  353. have no real features and are close to being a continous tone disappear
  354. and leave features that have a lot of contrast with their neighbors. This
  355. is the function of an edge detector. If the kernal has a sum of 0, it will
  356. enhance the edges of an image in a certain direction. In the laplacians case,
  357. the enhancement will take place in all directions. Here is why this will
  358. happen:
  359.  
  360.  If you have a 3x3 area of the image that all the pixels are equal to
  361.  the same same value, say all of the pixels are equal to 200, it will be
  362.  uniform in color and contain no edges. When the laplacian is passed
  363.  over this section, the result of the convolution will be 0, or the
  364.  absence of an edge. If you have an area the looks like:
  365.  
  366.                         100 100 100
  367.                         200 200 200
  368.                         250 250 250
  369.  
  370.  The result of the convolution with the laplacian would be 150, and there
  371.  would be an edge present.
  372.  
  373.  Take for example a run of pixels that looks like this: 0 150 200 That
  374.  area obviously contains an edge. On a graph it would look like this:
  375.  
  376.        /|255
  377.      /  |200
  378.    /    |150
  379.   0-----|
  380.  
  381.  If you used a 1x3 kernal with the values -1 0 1, the middle value would
  382.  become 200 ( (0x0=0) + (150x0=0) + (200x1=200)) and show the precence
  383.  of the edge. If the values of the run were 100 100 100, the area would
  384.  be uniform and the middle pixels value would become 0 and accurately
  385.  depict the uniform area. 
  386.  
  387. A horizontal kernel looks like this:
  388.                     
  389.                     Horizontal kernel:
  390.                       -1  -1  -1
  391.                        0   0   0
  392.                        1   1   1
  393.  
  394. You will notice that the horizontal kernal will only detect edges in the
  395. horizontal direction due to the direction of the of the kernal.
  396.  
  397.  
  398. A vertical kernel looks like this:
  399.  
  400.                     
  401.                     Vertical kernel:
  402.                       -1   0   1
  403.                       -1   0   1
  404.                       -1   0   1
  405.  
  406. The same goes for the vertical kernal, only the direction is in the vertical
  407. direction.
  408.  
  409. There are other methods of edge detecion available that IMPROCES does
  410. not implement at the present time. In fact there are new methods and
  411. filters being invented everyday. Future versions of IMPROCES will
  412. support convoling an area with a seperate filter for each direction and
  413. other forms of edge detection.
  414.  
  415. There is also a variable called BOOST that is used when using the
  416. convolution filters. BOOST is used to increase or decrease the amount of
  417. the filter that is applied to the original image. What happens is the
  418. pixels under the kernel are first multiplied by the corresponding
  419. kernel value and then are multiplied by the BOOST value. A BOOST value
  420. of less then 1.0 will lessen the effect the filter has, while a value
  421. greater then 1.0 will increase the effect.
  422.  
  423. IMPROCES includes a Custom Filter function that lets you define the
  424. kernel to use. You will also note that as of version 3.0 of IMPROCES,
  425. there are separate functions for greyscale images and for color images.
  426. The greyscale functions work a lot faster. The color functions must
  427. convol the RGB attributes of each pixel and then search the palette for
  428. the proper color to replace the pixel with. The color process rarely
  429. will find an exact match for the color that gets produced from the
  430. convolution, but it will find the closest possible match and use it.
  431. The grey functions need only convol the LUT value of the pixels and
  432. use the result to get an exact match.
  433.  
  434.  
  435. Average and Median:
  436. Two other filters that are included are the Average and Median filters.
  437. Both of these use a 3x3 matrix as before, but only the values in the
  438. image are used.
  439.  
  440. Average will find the average value of the pixels under the matrix and
  441. replace the center pixel with that value. Median will find the middle
  442. value used and use that. Both functions work the same for greyscale and
  443. color images.
  444.  
  445. An important note for all of the functions that use a matrix or a kernel
  446. is that the pixels on the edges (top, right, left, bottom) of the work
  447. area will not be affected by the functions. This is because these pixels
  448. do not have enough neighbors to be used in processing. In IMPROCES these
  449. pixels are simply left alone.
  450.  
  451. Wrap it up!:
  452. All in all, image processing is used in many fields; Astronomy,
  453. dentistry, forensic science and X-Ray to just name a few. Software with
  454. a price of $25 like IMPROCES and the price of personal computers
  455. equipped with VGA and SVGA hardware dropping like a rock, have made
  456. image processing available to the masses. It is exciting, useful and
  457. most of all fun.
  458.  
  459. I hope this little primer has peeked your interest in image processing
  460. and that it helps make some things that IMPROCES can do a little clearer.
  461. If for some reason you obtained this article without a copy of IMPROCES,
  462. the latest version of the program can be downloaded from the Dust Devil
  463. BBS in Las Vegas, Nevada, (702)796-7134. I can also be reached at the
  464. Dust Devil BBS if you have any questions about IMPROCES.
  465.